<template>
  <div class="layout">
    <el-container v-if="showMenu" class="layout-container">
      <el-aside class="layout-aside">
        <div class="layout-head">
          <div>
            <span>MicroCenter</span>
          </div>
        </div>
        <div class="layout-line" />
        <Aside />
      </el-aside>
      <el-container class="layout-content">
        <Header />
        <div class="layout-main">
          <router-view />
        </div>
      </el-container>
    </el-container>
    <el-container v-else class="layout-container">
      <router-view />
    </el-container>
  </div>
</template>

<script>
import { reactive, toRefs, onBeforeMount, onMounted, onUnmounted } from "vue";
import { useRouter } from "vue-router";
import Header from "./components/Header.vue";
import Aside from "./components/Aside.vue";
export default {
  name: "",
  components: {
    Header,
    Aside,
  },
  setup() {
    const noMenu = ["/login"];
    const state = reactive({
      showMenu: false,
    });
    const router = useRouter();
    const unwatch = router.beforeEach((to, from, next) => {
      if (to.path == "/login") {
        // 如果路径是 /login 则正常执行
        next();
      } else {
        const token = $.cache.token;
        if (!token) {
          // 如果没有，则跳至登录页面
          return next({ path: "/login" });
        } else {
          // 否则继续执行
          next();
        }
      }
      state.showMenu = !noMenu.includes(to.path);
    });

    onUnmounted(() => {
      unwatch();
    });

    onBeforeMount(() => {
     
    });
    onMounted(() => {
     
    });
    return {
      ...toRefs(state),
    };
  },
};
</script>
<style>
body {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
.layout {
  min-height: 100vh;
  background-color: #ffffff;
}
.layout-container {
  height: 100vh;
}
.layout-aside {
  width: 200px !important;
  background-color: #222832;
  overflow: hidden;
  overflow-y: auto;
  -ms-overflow-style: none;
  overflow: -moz-scrollbars-none;
}
.layout-aside::-webkit-scrollbar {
  display: none;
}

.layout-head {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
}
.layout-head > div {
  display: flex;
  align-items: center;
}
.layout-head img {
  width: 50px;
  height: 50px;
  margin-right: 10px;
}
.layout-head span {
  font-size: 20px;
  color: #ffffff;
}
.layout-line {
  border-top: 1px solid hsla(0, 0%, 100%, 0.05);
  border-bottom: 1px solid rgba(0, 0, 0, 0.2);
}
.layout-content {
  display: flex;
  flex-direction: column;
  max-height: 100vh;
  overflow: hidden;
}
.layout-main {
  height: calc(100vh - 100px);
  overflow: auto;
  padding: 10px;
}
</style>
